local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent(),script.Parent.Name);
local ContextActionService = game:GetService("ContextActionService")
local LanguageHelper=_G.LanguageHelper --1:购买，2装备，3卸载
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local Pet_Cfg=ClientOHOH.config.Pet
local Game_Cfg=ClientOHOH.config.Game
local PetTool=_G.CommonOHOH.load.PetTool();

-- local PetGui=ClientOHOH.load.UIManager().ui_list.PetGui
local HomeGui=ClientOHOH.load.UIManager().ui_list.HomeGui

 _G.ClientOHOH.load.UIManager():CreateUI("PetGoldGui",require(script.PetGoldGui))
local PetGui= _G.ClientOHOH.load.UIManager():CreateUI("PetGui",require(script.PetGui))
_G.ClientOHOH.load.UIManager():CreateUI("PetVoidGui",require(script.PetVoidGui))
function M.new(self,...)
	return M(self,...);
end;

local FunctionList = ClientOHOH.load.FunctionList();

function M.GetEquipCount(self)
    local count=0
    for k,v in pairs(self.DataBase:GetData("pets").equip) do
        count=count+v
    end
    return count
end

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.player=self.parent
    self.DataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());

    ClientOHOH.load.CallBackManager():AddRbxConnect(self,self.player.ExpValueMulEvent, function()
        local sum=0
        local OnwedPet=self.DataBase:GetData("pets").had
         for k,v in pairs(self.DataBase:GetData("pets").equip) do
            sum=sum+PetTool.GetPetBoostValueByPetId(k,OnwedPet)*v
         end
         return sum
	end);


	local data=self.DataBase:GetData("pets")
    local petsPack=self.DataBase:GetData("petsPack")


    self.OldCount=0
    for k,v in pairs(data.had) do
        self.OldCount= self.OldCount+v
    end

    --调用顺序不可更改
    PetGui:SetDataBase(self.DataBase)
    PetGui:UpdateHad(data.had)
    PetGui:UpdateEquip(data.equip)
    PetGui:UpdateEquipSlotsInfo(petsPack.equipMax,Game_Cfg.FullEquipCount)
    PetGui:UpdatePackInfo(petsPack.backpack,Game_Cfg.FullBackpackCount)

	self.UserDataEventConnect=self.parent.UserDataEvent:Connect(function(data)

        -- 调用顺序不可更改
		if data["pets"] then
            PetGui:UpdateHad(data["pets"].had)
            PetGui:UpdateEquip(data["pets"].equip)

            local nowCout=0
            for k,v in pairs(data["pets"].had) do
                nowCout= nowCout+v
            end
            local d=nowCout-self.OldCount
            self.OldCount=nowCout
            if d>0 then
                HomeGui:SetPetRed(d)
            end
		end
        if data["petsPack"] then
            PetGui:UpdateEquipSlotsInfo(data["petsPack"].equipMax,Game_Cfg.FullEquipCount)
            PetGui:UpdatePackInfo(data["petsPack"].backpack,Game_Cfg.FullBackpackCount)
        end

	end)

    PetGui.SellEvent:Connect(function(list)
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_Delete,list)
    end)

    PetGui.MergeOneEvent:Connect(function(petsId)
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_MergeOne,petsId)
    end)

    PetGui.MergeAllEvent:Connect(function()
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_MergeAll)
    end)

    
    PetGui.BestEquipEvent:Connect(function()
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_EquipBest)
    end)

    PetGui.EquipEvent:Connect(function(list)
        if self:GetEquipCount()+#list>self.DataBase:GetData("petsPack").equipMax then
            GlobalTipGui:Show(LanguageHelper:GetText(65))
            return false
        end
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_Equip,list)
    end)

    PetGui.UnEquipEvent:Connect(function(list)
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_UnEquip,list)
    end)

    PetGui.UnEquipAllEvent:Connect(function()
        return ClientOHOH.function_manager:Invoke(ClientOHOH.function_list.Pets_UnEquipAll)
    end)

    PetGui.BuyPackEvent:Connect(function()
         self.player.RobuxBuyEvent:Action(Game_Cfg.GoodsId.PetPackGid_1)
    end)

    PetGui.BuyEquipEvent:Connect(function()
        local nowEquipCount=self.DataBase:GetData("petsPack").equipMax
        for _,v in pairs(Game_Cfg.BuyEquip) do
            if nowEquipCount<v.count then
                self.player.RobuxBuyEvent:Action(v.gid)
                break
            end
        end
    end)

    self.player:AddPrivateComponentByFolder(script.PrivateCtrl)
end;

function M.dtor(self)
	M.super.dtor(self);
end;



return M